//
//  ApiClient+Ext.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/17.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ApiClient+Ext.h"
#import "AppContext.h"
#import "WXApi.h"

@implementation ApiClient (Ext)

+ (void)requestWithPath:(NSString *)path params:(NSMutableDictionary *)params  imageDatas:(NSString*)imageDatas  httpMethod:(NSString*)httpMethod  didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail
{
    
    NSAssert(path!=nil, @"the url path can't be null");
    NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded;charset=utf-8;", @"Content-Type", @"2",@"_p",[[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey],@"_v",[[[UIDevice currentDevice] macaddressOrIDFA] stringByReplacingOccurrencesOfString:@":" withString:@""],@"_m",nil];
    if (APP_CONTEXT.account.data.token) {
        [headers setValue:APP_CONTEXT.account.data.token forKey:@"X-Oc-Session"];
    }

    MKNetworkOperation *operation = [ApiClient requestWithPath:path headers:headers params: params httpMethod:httpMethod];
    [operation addFile:imageDatas forKey:@"file"];
       [operation onUploadProgressChanged:^(double progresNum) {
        
        QLKDLog(@"上传的进度%.2f",progresNum*100);
       
    }];
    
    [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        QLKDLog(@"responseString=%@", completedOperation.responseString);
        KXJson *result = [KXJson jsonWithJsonString:completedOperation.responseString];
        if (finish) {
            finish(result, path);
        }
        
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        
        //        [ATMHud hideActivity];
        //
        //        [ATMHud showMessage:kErrNetworkStatus];
        
        if (fail) {
            fail(error, path);
        }
    }];
    [[ApiClient engine] enqueueOperation:operation];

    
   
}


+ (void)requestWithPath:(NSString *)path params:(NSMutableDictionary *)params httpMethod:(NSString*)httpMethod didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail
{
    
//    [[LKDHud shareHud] showLoadingView];
    NSAssert(path!=nil, @"the url path can't be null");
    NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"application/x-www-form-urlencoded;charset=utf-8;", @"Content-Type", @"2",@"_p",[[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey],@"_v",[[[UIDevice currentDevice] macaddressOrIDFA] stringByReplacingOccurrencesOfString:@":" withString:@""],@"_m",nil];
    if (APP_CONTEXT.account.data.token) {
        [headers setValue:APP_CONTEXT.account.data.token forKey:@"X-Oc-Session"];
    }
//    [params setValue:@"ios" forKey:@"os"];
//    [params setValue:[[UIDevice currentDevice] macaddressOrIDFA] forKey:@"idfa"];
//    [params setValue:[[UIDevice currentDevice] macaddress] forKey:@"mac"];
    //    QLKDLog(@"请求接口传入参数===%@",params);
    MKNetworkOperation *operation = [ApiClient requestWithPath:path headers:headers params: params httpMethod:httpMethod];
    
    //[operation addData:imageData forKey:@"file_name" mimeType:@"image/png" fileName:@"image.png"];  //图片
    
    [operation addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        NSLog(@"responseString===============%@", completedOperation.responseString);
        KXJson *result = [KXJson jsonWithJsonString:completedOperation.responseString];
        [[LKDHud shareHud] hideLoadingView];
        if (finish) {
            if ([result getIntForKey:@"status"]!=1) {
                NSString *errorMsg = [result getStringForKey:@"message"];
                if ((![errorMsg isEqualToString:@"未登录"]) &&  (errorMsg.length > 0)) {
                    [[[LKDHud alloc] init] showtip:[result getStringForKey:@"message"]];
                }
            }
            finish(result, path);
        }
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        
        if (fail) {
            fail(error, path);
        }
    }];
    [[ApiClient engine] enqueueOperation:operation];
}



+(void)getHomeDataDidFinishLoaded:(homeModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_home params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        if ([[json getStringForKey:@"status"]intValue]==1) {
            KXJson *dataJson=[json getJsonForKey:@"data"];
            KXJson *modulesJson=[dataJson getJsonForKey:@"modules"];
            HomeModel *homeModel=[[HomeModel alloc] init];
            [homeModel formatData:modulesJson];
            finish(homeModel,path);
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}
+ (void)getSearchHotWordDidFinishLoaded:(searchHotWordModel)finish didFailLoaded:(didFailLoaded)fail
{
    [ApiClient requestWithPath:API_path_search params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        SearchHotWordModel *model=[[SearchHotWordModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

    
}

+ (void)getSearchResultKeyword:(NSString*)keyword Sort:(NSString*)sort order:(NSString*)order page:(NSString*)page DidFinishLoaded:(searchResultModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:keyword forKey:@"keyword"];
    [paramsDict setObject:sort forKey:@"sort"];
    [paramsDict setObject:order forKey:@"order"];
    [paramsDict setObject:page forKey:@"page"];
    [ApiClient requestWithPath:API_path_search params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        SearchResultModel *model=[[SearchResultModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}


+(void)registerNewUserTelephone:(NSString*)telephone sms:(NSString*)sms password:(NSString*)password DidFinishLoaded:(accountModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:telephone forKey:@"telephone"];
    [paramsDict setObject:sms forKey:@"sms"];
    [paramsDict setObject:password forKey:@"password"];
    [ApiClient requestWithPath:API_path_register params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        AccountModel *model=[[AccountModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)sendRegiseterSmsTelephone:(NSString*)tel didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:tel forKey:@"telephone"];
    [ApiClient requestWithPath:API_path_register_sms params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];
}






+(void)loginWithAccount:(NSString*)account password:(NSString*)password didFinishLoaded:(accountModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:account forKey:@"account"];
    [paramsDict setObject:password forKey:@"password"];
    
    [ApiClient requestWithPath:API_path_login params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        AccountModel *model=[[AccountModel alloc] initWithDictionary:json.json];
        finish(model,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)loginOutDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_logout params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];

}

+(void)forgetPasswordWithPhone:(NSString*)phone sms:(NSString*)sms password:(NSString*)password didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:phone forKey:@"telephone"];
    [paramsDict setObject:sms forKey:@"sms"];
    [paramsDict setObject:password forKey:@"password"];
    [ApiClient requestWithPath:API_path_forgotten params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];
    

}

+(void)sendForgottenSmsTelephone:(NSString*)tel didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:tel forKey:@"telephone"];
    [ApiClient requestWithPath:API_path_forgotten_sms params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];

}


+(void)getAllCategoriesDidFinishLoaded:(classModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_categoriesAll params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        ClassModel *model=[[ClassModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}
+(void)getAllFirstCategoriesDidFinishLoaded:(firstCategoryModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_categories params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        FirstCategoryModel *model=[[FirstCategoryModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getProductListCategory_id:(NSString*)category_id sort:(NSString*)sort order:(NSString*)order page:(NSString*)page didFinishLoaded:(prodcutListCategoryModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:category_id forKey:@"category_id"];
    [paramsDict setObject:sort forKey:@"sort"];
     [paramsDict setObject:order forKey:@"order"];
     [paramsDict setObject:page forKey:@"page"];
    [ApiClient requestWithPath:API_path_categories_products params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        ProdcutListCategoryModel *model=[[ProdcutListCategoryModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)getAllBrandsDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_brands_all params:paramsDict httpMethod:@"GET" didFinishLoaded:finish didFailLoaded:fail];

}

+(void)getProductBrand_id:(NSString*)brand_id sort:(NSString*)sort order:(NSString*)order page:(NSString*)page didFinishLoaded:(prodcutListCategoryModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:brand_id forKey:@"brand_id"];
    [paramsDict setObject:sort forKey:@"sort"];
    [paramsDict setObject:order forKey:@"order"];
    [paramsDict setObject:page forKey:@"page"];
    [ApiClient requestWithPath:API_path_brands_products params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        ProdcutListCategoryModel *model=[[ProdcutListCategoryModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

    
}

+(void)getBrandsGroupedDidFinishLoaded:(brandGroupModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_brands_grouped params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        BrandGroupModel *model=[[BrandGroupModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)getBrandsGroupZiMuDidFinishLoaded:(brandsGroupZiMuModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_brands_alphabet params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        BrandsGroupZiMuModel *model=[[BrandsGroupZiMuModel alloc] initWithDictionary:json.json];
        finish(model,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getAccountInfoDidFinishLoaded:(accountModel)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_account_info params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        AccountModel *model=[[AccountModel alloc] initWithDictionary:json.json];
        finish(model,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}
#pragma mark-获取收藏列表
+(void)getCollectionListPage:(NSString*)page didFinishLoaded:(collectionModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"account/wishlist/%@",page];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CollectionModel *model = [[CollectionModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];

}
#pragma mark-已评论的商品列表
+(void)getHaveCommentListPageNumber:(NSString*)page didFinishLoaded:(commentListModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"review/%@",page];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CommentListModel *model = [[CommentListModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];

}
+(void)getWaitCommentListPageNumber:(NSString*)page didFinishLoaded:(judgeModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"review/wait/%@",page];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        JudgeModel *model = [[JudgeModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];

}
+(void)getInvitationInfoDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    
    [ApiClient requestWithPath:API_path_account_invitation params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        NSLog(@"json:%@", json);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}
+(void)saveInvitationCode:(NSString*)code DidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:code forKey:@"code"];
    [ApiClient requestWithPath:API_path_account_invitation params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        NSLog(@"json:%@", json);
    }didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
}
+(void)bindInvitationCodeDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_account_invitation params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        NSLog(@"json:%@", json);
    }didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
}
     
#pragma 获取城市列表
+(void)getProvinceAndCityList:(areaListModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_area_list params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        AreaModel *model=[[AreaModel alloc] initWithDictionary:json.json];
        finish(model,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getAccountOrdersListOrderType:(NSString*)type page:(NSString*)page didFinishLoaded:(accountOrdersModel)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:type forKey:@"type"];
    [paramsDict setObject:page forKey:@"page"];
    [ApiClient requestWithPath:API_path_account_orders params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        AccountOrdersModel *model=[[AccountOrdersModel alloc] initWithDictionary:json.json];
        finish(model, path);
    }didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
}
+(void)getOrderDetailOrderID:(NSString*)orderID didFinishLoaded:(orderDetailModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"account/orders/%@",orderID];
    [ApiClient requestWithPath:path params:nil
                    httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
            OrderDetailModel *model=[[OrderDetailModel alloc] initWithDictionary:json.json];
                        finish(model, path);
                        
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}
+(void)comfirmOrder_finishOrdierID:(NSString*)orderID didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"account/order_finish/%@",orderID];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:finish didFailLoaded:fail];

}
+(void)getExpressDetailOrderID:(NSString *)orderID didFinishLoaded:(expressInfoModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"%@%@",API_path_order_express,orderID];
    [ApiClient requestWithPath:path params:nil
                    httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
                        ExpressInfoModel *model=[[ExpressInfoModel alloc] initWithDictionary:json.json];
                        finish(model, path);
                        
                    } didFailLoaded:^(NSError *error, NSString *path) {
                        fail(error,path);
                    }];

}
#pragma mark-添加评论
+(void)addReviewOrder_product_id:(NSString*)Order_product_id product_id:(NSString*)product_id review:(NSString*)review rating:(NSString*)rating didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:Order_product_id forKey:@"order_product_id"];
    [paramsDict setObject:product_id forKey:@"product_id"];
    [paramsDict setObject:review forKey:@"review"];
     [paramsDict setObject:rating forKey:@"rating"];
    [ApiClient requestWithPath:API_path_review_add params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];
    
}

+(void)changeFirstName:(NSString*)firstname didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:firstname forKey:@"firstname"];
    [ApiClient requestWithPath:API_path_account_firstname params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];

}

+(void)changeGender:(NSString*)gender didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:gender forKey:@"gender"];
    [ApiClient requestWithPath:API_path_account_gender params:paramsDict httpMethod:@"POST" didFinishLoaded:finish didFailLoaded:fail];
    
}
+(void)cancelOrder_ID:(NSString*)order_id didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"account/order_cancel/%@",order_id];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:finish didFailLoaded:fail];

}


+(void)orderPayOrder_id:(NSString*)order_id didFinishLoaded:(orderPayModel)finish didFailLoaded:(didFailLoaded)fail{
    NSString *path = [NSString stringWithFormat:@"%@/%@", API_path_account_order_pay, order_id];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        OrderPayModel *model=[[OrderPayModel alloc] initWithDictionary:json.json];
        finish(model,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

     
#pragma mark-收货地址
 +(void)getAddressListDidFinishLoaded:(addressListModel)finish didFailLoaded:(didFailLoaded)fail {
     [ApiClient requestWithPath:API_path_address_list params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
         AddressListModel *model=[[AddressListModel alloc] initWithDictionary:json.json];
         finish(model,path);
     } didFailLoaded:^(NSError *error, NSString *path) {
         fail(error,path);
     }];
 }

+(void)setDefaultAddress:(NSString *)addressId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:addressId forKey:@"address_id"];
    [ApiClient requestWithPath:API_path_address_setdefualt params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)deleteAddress:(NSString *)addressId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:addressId forKey:@"address_id"];
    [ApiClient requestWithPath:API_path_address_delete params:paramsDict httpMethod:@"DELETE" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)addAddress:(NSMutableDictionary *)params didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_address_add params:params httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

#pragma mark-获取优惠券列表

+(void)getCouponListFinishLoaded:(couponListModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_coupon_list params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CouponListModel *model = [[CouponListModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

+(void)getCheckoutCouponListFinishLoaded:(couponListModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_checkout_coupon_list params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CouponListModel *model = [[CouponListModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

#pragma mark-获取产品详情
+(void)getProductDetailInfo:(NSString *)productId didFinishLoaded:(productDetailModel)finish didFailLoaded:(didFailLoaded)fail {
    NSString *path = [NSString stringWithFormat:@"%@%@", API_path_product_detail_prefix, productId];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        ProdcutDetaiModel *model = [[ProdcutDetaiModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}
//获取产品评论列表
+(void)getProductCommentList:(NSString *)productId pageNumber:(NSInteger)page didFinishLoaded:(commentListModel)finish didFailLoaded:(didFailLoaded)fail {
    NSString *path = [NSString stringWithFormat:@"products/%@/reviews/%ld",productId,page];
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CommentListModel *model = [[CommentListModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

#pragma mark-加入购物车
+(void)addProductToShopBag:(NSString *)productId quantity:(NSInteger)quantity options:(NSDictionary *)option didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:productId forKey:@"product_id"];
    [paramsDict setObject:@(quantity) forKey:@"quantity"];
    if(option.count > 0) {
        [paramsDict setObject:option forKey:@"option"];
    }

    [ApiClient requestWithPath:API_path_add_product_to_cart params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getProductNumberInCart:(cartCountModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_cart_count params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CartCountModel *model = [[CartCountModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getCardListDidfinishLoaded:(cartListModel)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_cart_products params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CartListModel *model = [[CartListModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)selectProductToPay:(NSString *)cartId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSString *path = [NSString stringWithFormat:@"cart/check/%@", cartId];
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:cartId forKey:@"cart_id"];
    [ApiClient requestWithPath:path params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)unSelectProductToPay:(NSString *)cartId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSString *path = [NSString stringWithFormat:@"cart/uncheck/%@", cartId];
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:cartId forKey:@"cart_id"];
    [ApiClient requestWithPath:path params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)updateProductQuantityInCart:(NSString *)cartId quantity:(NSInteger)quantity didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:cartId forKey:@"cart_id"];
    [paramsDict setObject:@(quantity) forKey:@"quantity"];
    [ApiClient requestWithPath:API_path_cart_update_product_quantity params:paramsDict httpMethod:@"PUT" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)selectAllProductToCart:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_cart_select_all params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)unSelectAllProductToCart:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_cart_unselect_all params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)deleteProductIncard:(NSString *)cartId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:cartId forKey:@"cart_id"];
    [ApiClient requestWithPath:API_path_cart_delete_product params:paramsDict httpMethod:@"DELETE" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

#pragma mark--结算
+(void)setShippingAddress:(NSString *)addressId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:addressId forKey:@"address_id"];
    [ApiClient requestWithPath:API_path_checkout_address params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)setShippingCoupon:(NSString *)couponCode didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:couponCode forKey:@"code"];
    [ApiClient requestWithPath:API_path_checkout_coupon params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)deleteShippingCouponDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_checkout_coupon params:nil httpMethod:@"DELETE" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)setPayment:(NSString *)pay didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:pay forKey:@"payment_method"];
    [ApiClient requestWithPath:API_path_checkout_pay params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)confirmOrderDidFinishLoaded:(orderConfirmModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_confirm_order params:nil httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        PayModel *model = [[PayModel alloc] initWithDictionary:json.json];
        finish(model,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)getCheckoutInfoDidFinishLoaded:(checkoutModel)finish didFailLoaded:(didFailLoaded)fail {
    NSString *path = [NSString stringWithFormat:@"%@%@", API_path_checkout_init, @"&weixin_installed=1"];
    if (![WXApi isWXAppInstalled]) {
        path = [NSString stringWithFormat:@"%@%@", API_path_checkout_init, @"&weixin_installed=0"];
    }
    [ApiClient requestWithPath:path params:nil httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        CheckoutModel *model = [[CheckoutModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

#pragma mark-立即购买
+(void)purchaseProduct:(NSMutableDictionary *)dictionary didFinishLoaded:(checkoutModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_purchase_product_now params:dictionary httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        CheckoutModel *model = [[CheckoutModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

+(void)getProductPrice:(NSMutableDictionary *)dictionary didFinishLoaded:(productPriceModel)finish didFailLoaded:(didFailLoaded)fail {
    [ApiClient requestWithPath:API_path_product_price params:dictionary httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        ProductPriceModel *model = [[ProductPriceModel alloc] initWithDictionary:json.json];
        finish(model, path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error, path);
    }];
}

#pragma mark-收藏商品
+(void)addProductToWishList:(NSString *)productId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:productId forKey:@"product_id"];
    [ApiClient requestWithPath:API_path_account_wish params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

+(void)deleteProductFromWishList:(NSString *)productId didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail {
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:productId forKey:@"product_id"];
    [ApiClient requestWithPath:API_path_account_wish params:paramsDict httpMethod:@"DELETE" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}

#pragma mark-上传图片
+(void)upLoadTuCaoImages:(NSString*)imagePath didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
     NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
     [ApiClient requestWithPath:API_path_feedback_upload_image params:paramsDict imageDatas:imagePath httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
          finish(json,path);
         
     } didFailLoaded:^(NSError *error, NSString *path) {
         
     }];

}

+(void)addTuCaoMobile:(NSString*)mobile content:(NSString*)content images:(NSArray*)images didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [paramsDict setObject:mobile forKey:@"telephone"];
    [paramsDict setObject:content forKey:@"comment"];
    [paramsDict setObject:images forKey:@"images"];
    [ApiClient requestWithPath:API_path_feedback params:paramsDict httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)upLoadHeadImage:(NSString *)imagePath didFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_account_avatar params:paramsDict imageDatas:imagePath httpMethod:@"POST" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        
    }];

}
+(void)getSplashDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_splash params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];

}

+(void)getCurrentVersionDidFinishLoaded:(didFinishLoaded)finish didFailLoaded:(didFailLoaded)fail{
    NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
    [ApiClient requestWithPath:API_path_version params:paramsDict httpMethod:@"GET" didFinishLoaded:^(KXJson *json, NSString *path) {
        finish(json,path);
    } didFailLoaded:^(NSError *error, NSString *path) {
        fail(error,path);
    }];
}
@end
